package client;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.ServiceFactory;

import oracle.bi.webservices.v6.AuthResult;
import oracle.bi.webservices.v6.QueryResults;
import oracle.bi.webservices.v6.ReportEditingService;
import oracle.bi.webservices.v6.ReportEditingServiceSoap;
import oracle.bi.webservices.v6.ReportRef;
import oracle.bi.webservices.v6.SAWSessionParameters;
import oracle.bi.webservices.v6.SAWSessionService;
import oracle.bi.webservices.v6.SAWSessionServiceSoap;
import oracle.bi.webservices.v6.SAWSessionServiceSoapProxy;
import oracle.bi.webservices.v6.XMLQueryExecutionOptions;
import oracle.bi.webservices.v6.XMLQueryOutputFormat;
import oracle.bi.webservices.v6.XmlViewService;
import oracle.bi.webservices.v6.XmlViewServiceSoap;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("BEGIN");
		SAWSessionParameters sessionParams = new SAWSessionParameters();
		sessionParams.setUserAgent("OBIEE WebService-driven Test"); //Copy the exact agent from your Firefox menu Help > About

		SAWSessionServiceSoap sessionService = null;
		String sessionID = "";

		SAWSessionServiceSoap sessionService2 = null;
		String sessionID2 = "";

		SAWSessionServiceSoapProxy sessionService3 = null;
		String sessionID3 = "";
		//path: /shared/Procurement and Spend/BSS R2 Procurement/Reports/Spend Trend by Top Categories

		try {
			javax.xml.rpc.ServiceFactory factory = ServiceFactory.newInstance();
			System.out.println("Before sessionService loading");
			sessionService = ((SAWSessionService) factory.loadService(SAWSessionService.class)).getSAWSessionServiceSoap();
			System.out.println("sessionService Loading finished");

			AuthResult authResult = sessionService.logonex("nbnbiwobiee_weblogic", "nbnbiwobiee_weblog1c", sessionParams); //You should reuse the session for multiple HTTP Requests from the same user
			sessionID = authResult.getSessionID();

			//HtmlViewServiceSoap htmlService = ((HtmlViewService) factory.loadService(HtmlViewService.class)).getHtmlViewService();
			//StartPageParams pageParams = new StartPageParams();
			//pageParams.setIdsPrefix("replace_with_your_prefix");
			//String pageID = htmlService.startPage(pageParams, sessionID);

			ReportRef report = new ReportRef();
			report.setReportPath("/shared/Usage Tracking/Usage Monitoring Reports/Average Performing Reports");
			//htmlService.addReportToPage(pageID, "Average_Performing_Reports", report, null, null, null, sessionID);
			//String reportHTML = htmlService.getHtmlForReport(pageID, "Average_Performing_Reports", sessionID);
			//System.out.println(reportHTML);
			//htmlService.endPage(pageID, sessionID);

			ReportEditingServiceSoap reportEdit = ((ReportEditingService)factory.loadService(ReportEditingService.class)).getReportEditingServiceSoap();

			String query = reportEdit.generateReportSQL(report, null, sessionID);
			System.out.println("Query: " + query);

			XMLQueryExecutionOptions options = new XMLQueryExecutionOptions();
			XmlViewServiceSoap xmlService = ((XmlViewService)factory.loadService(XmlViewService.class)).getXmlViewServiceSoap();
			QueryResults results = xmlService.executeSQLQuery(query, XMLQueryOutputFormat.SAWRowsetData, options, sessionID);

			//System.out.println("*" + results.getRowset() + "*");
			PrintWriter out = new PrintWriter("filename.txt");
			out.println(results.getRowset());
			out.close();

			//			WebCatalogServiceSoap webcat = ((WebCatalogService)factory.loadService(WebCatalogService.class)).getWebCatalogServiceSoap();
			//			GetSubItemsParams params = new GetSubItemsParams();
			//			ItemInfo[] items = webcat.getSubItems("/system", "*", false, params, sessionID);
			//			System.out.println("# of System contents: " + items.length);
			//			for (int i=0; i<items.length; i++)
			//				System.out.println("Item #" + i + ":\t" + items[i].getCaption());
			//			
			//			System.out.println("\n");
			//			items = webcat.getSubItems("/shared", "*", true, params, sessionID);
			//			System.out.println("# of Shared contents: " + items.length);
			//			for (int i=0; i<items.length; i++)
			//				System.out.println("Item #" + i + ":\t" + items[i].getCaption() + "\t" + items[i].getOwner());

			sessionService2 = new SAWSessionServiceSoapProxy("http://svbiw0000042np.nbndc.local:9704/analytics/saw.dll?SoapImpl=nQSessionService");
			AuthResult authResult2 = sessionService2.logonex("weblogic", "weblog1c", sessionParams); //You should reuse the session for multiple HTTP Requests from the same user
			if (authResult2.isAuthCompleted()) {
				sessionID2 = authResult2.getSessionID();
				System.out.println("Session ID: " + sessionID2);
			}

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		} finally {
			if (sessionService != null && !sessionID.equals("")) {
				System.out.println("Logging off");
				sessionService.logoff(sessionID);
				sessionService2.logoff(sessionID2);
			}
		}
		System.out.println("END");
	}

}
